package com.ihr360.payroll.controller.salary;

import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.entity.setting.SalaryCompanySettingEntity;
import com.ihr360.payroll.model.dto.salary.SalaryCompanySetting;
import com.ihr360.payroll.service.salary.SalaryCompanySettingService;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公司薪资设置
 *
 * @author tomtao
 */
@RestController
@RequestMapping("/salaryCompanySettings")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.SALARY_SETTING_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.SALARY_SETTING_NAME,
        hasExtendsAbstractRestController = true)
public class SalaryCompanySettingController extends AbstractRestSimpleWithLongIdController<SalaryCompanySetting> {

    @Autowired
    private SalaryCompanySettingService salaryCompanySettingService;

    @Override
    public SimpleRestService<SalaryCompanySetting, Long> getSimpleRestService() {
        return salaryCompanySettingService;
    }

    @FunctionDefinition(functionName = "获取设置信息", functionCode = ModuleCodeStore.Function.COMPANY_SETTING)
    @GetMapping(value = "/getSalaryCompanySetting")
    public ResponseEntity getSalaryCompanySetting(@RequestParam(required = false) String projection, AuthUserInfo authUserInfo) {
        SalaryCompanySetting salaryCompanySetting = salaryCompanySettingService.getSalaryCompanySetting(authUserInfo.getCompanyId());
        if (salaryCompanySetting != null) {
            return toResource(salaryCompanySetting, projection);
        } else {
            return null;
        }
    }

    @GetMapping(value = "/querySalaryCompanySetting")
    @DisableResponseAdvice
    @FunctionDefinition(functionName = "查询公司薪资审批设置", functionCode = ModuleCodeStore.Function.COMPANY_SETTING)
    public SalaryCompanySettingEntity querySalaryCompanySetting(@RequestParam(value = "companyId") String companyId) {
        SalaryCompanySetting salaryCompanySetting = salaryCompanySettingService.getSalaryCompanySetting(companyId);
        if (salaryCompanySetting == null) {
            return null;
        } else {
            SalaryCompanySettingEntity entity = new SalaryCompanySettingEntity();
            BeanUtils.copyProperties(salaryCompanySetting, entity);
            return entity;
        }
    }

}
